<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '':'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem :label="t('procure.purchorderdetails.orderno')" type="remoteSelect" v-model="baTable.form.items!.orderno" prop="orderno" :input-attr="{ pk: 'cncerp_procure_purchorders.orderno', field: 'comments', 'remote-url': '/admin/procure.Purchorders/index' }" :placeholder="t('Please select field', { field: t('procure.purchorderdetails.orderno') })" />
                    <FormItem :label="t('procure.purchorderdetails.itemcode')" type="remoteSelect" v-model="baTable.form.items!.itemcode" prop="itemcode" :input-attr="{ pk: 'cncerp_.id', field: 'name', 'remote-url': '' }" :placeholder="t('Please select field', { field: t('procure.purchorderdetails.itemcode') })" />
                    <FormItem :label="t('procure.purchorderdetails.deliverydate')" type="date" v-model="baTable.form.items!.deliverydate" prop="deliverydate" :placeholder="t('Please select field', { field: t('procure.purchorderdetails.deliverydate') })" />
                    <FormItem :label="t('procure.purchorderdetails.itemdescription')" type="string" v-model="baTable.form.items!.itemdescription" prop="itemdescription" :placeholder="t('Please input field', { field: t('procure.purchorderdetails.itemdescription') })" />
                    <FormItem :label="t('procure.purchorderdetails.glcode')" type="remoteSelect" v-model="baTable.form.items!.glcode" prop="glcode" :input-attr="{ pk: 'cncerp_fm_chartmaster.accountcode', field: 'accountname', 'remote-url': '/admin/fm.Chartmaster/index' }" :placeholder="t('Please select field', { field: t('procure.purchorderdetails.glcode') })" />
                    <FormItem :label="t('procure.purchorderdetails.qtyinvoiced')" type="number" prop="qtyinvoiced" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.qtyinvoiced" :placeholder="t('Please input field', { field: t('procure.purchorderdetails.qtyinvoiced') })" />
                    <FormItem :label="t('procure.purchorderdetails.unitprice')" type="number" prop="unitprice" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.unitprice" :placeholder="t('Please input field', { field: t('procure.purchorderdetails.unitprice') })" />
                    <FormItem :label="t('procure.purchorderdetails.actprice')" type="number" prop="actprice" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.actprice" :placeholder="t('Please input field', { field: t('procure.purchorderdetails.actprice') })" />
                    <FormItem :label="t('procure.purchorderdetails.stdcostunit')" type="number" prop="stdcostunit" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.stdcostunit" :placeholder="t('Please input field', { field: t('procure.purchorderdetails.stdcostunit') })" />
                    <FormItem :label="t('procure.purchorderdetails.quantityord')" type="number" prop="quantityord" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.quantityord" :placeholder="t('Please input field', { field: t('procure.purchorderdetails.quantityord') })" />
                    <FormItem :label="t('procure.purchorderdetails.quantityrecd')" type="number" prop="quantityrecd" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.quantityrecd" :placeholder="t('Please input field', { field: t('procure.purchorderdetails.quantityrecd') })" />
                    <FormItem :label="t('procure.purchorderdetails.shiptref')" type="number" prop="shiptref" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.shiptref" :placeholder="t('Please input field', { field: t('procure.purchorderdetails.shiptref') })" />
                    <FormItem :label="t('procure.purchorderdetails.jobref')" type="string" v-model="baTable.form.items!.jobref" prop="jobref" :placeholder="t('Please input field', { field: t('procure.purchorderdetails.jobref') })" />
                    <FormItem :label="t('procure.purchorderdetails.completed')" type="number" prop="completed" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.completed" :placeholder="t('Please input field', { field: t('procure.purchorderdetails.completed') })" />
                    <FormItem :label="t('procure.purchorderdetails.suppliersunit')" type="string" v-model="baTable.form.items!.suppliersunit" prop="suppliersunit" :placeholder="t('Please input field', { field: t('procure.purchorderdetails.suppliersunit') })" />
                    <FormItem :label="t('procure.purchorderdetails.suppliers_partno')" type="string" v-model="baTable.form.items!.suppliers_partno" prop="suppliers_partno" :placeholder="t('Please input field', { field: t('procure.purchorderdetails.suppliers_partno') })" />
                    <FormItem :label="t('procure.purchorderdetails.assetid')" type="number" prop="assetid" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.assetid" :placeholder="t('Please input field', { field: t('procure.purchorderdetails.assetid') })" />
                    <FormItem :label="t('procure.purchorderdetails.conversionfactor')" type="number" prop="conversionfactor" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.conversionfactor" :placeholder="t('Please input field', { field: t('procure.purchorderdetails.conversionfactor') })" />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type { FormInstance, FormItemRule } from 'element-plus'
import { inject, reactive, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import { useConfig } from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import { buildValidatorData } from '/@/utils/validate'

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    orderno: [buildValidatorData({ name: 'number', title: t('procure.purchorderdetails.orderno') })],
    deliverydate: [buildValidatorData({ name: 'date', title: t('procure.purchorderdetails.deliverydate') })],
    qtyinvoiced: [buildValidatorData({ name: 'number', title: t('procure.purchorderdetails.qtyinvoiced') })],
    unitprice: [buildValidatorData({ name: 'number', title: t('procure.purchorderdetails.unitprice') })],
    actprice: [buildValidatorData({ name: 'number', title: t('procure.purchorderdetails.actprice') })],
    stdcostunit: [buildValidatorData({ name: 'number', title: t('procure.purchorderdetails.stdcostunit') })],
    quantityord: [buildValidatorData({ name: 'number', title: t('procure.purchorderdetails.quantityord') })],
    quantityrecd: [buildValidatorData({ name: 'number', title: t('procure.purchorderdetails.quantityrecd') })],
    shiptref: [buildValidatorData({ name: 'number', title: t('procure.purchorderdetails.shiptref') })],
    completed: [buildValidatorData({ name: 'number', title: t('procure.purchorderdetails.completed') })],
    assetid: [buildValidatorData({ name: 'number', title: t('procure.purchorderdetails.assetid') })],
    conversionfactor: [buildValidatorData({ name: 'number', title: t('procure.purchorderdetails.conversionfactor') })],
})
</script>

<style scoped lang="scss"></style>
